print('Press 1,2,3,4 to select the games')
print('1：深渊历险；2：勇闯BUBU星；3：穿越大峡谷；4：井字棋')
x = input()
if x == '1':
    import pgzrun
    import random
    print('当游戏失败时，可以按空格键重新开始游戏')
    WIDTH = 500
    HEIGHT = 700

    dudu = Actor('嘟嘟')
    score = 0
    bricks = []
    for i in range(5):
        n = random.randint(1, 4)
        b = Actor('踏板' + str(n))
        min_x = b.width // 2
        max_x = WIDTH - b.width // 2
        b.x = random.randint(min_x, max_x)
        b.y = 140 * (i + 1)
        bricks.append(b)
        if i == 2:
            dudu.x = b.x
            dudu.bottom = b.top
            b.image = '踏板1'


    def draw():
        global score
        screen.blit('背景', [0, 0])
        dudu.draw()
        for brick in bricks:
            brick.draw()
        screen.draw.text(str(score), (20, 10))

    def update():
        global score
        if dudu.image == '嘟嘟':
            score += 1
            on_brick = 0
            for b in bricks:
                b.y -= 3
                if b.y < 0:
                    n = random.randint(1, 4)
                    b.image = '踏板' + str(n)
                    b.y = HEIGHT
                    min_x = b.width // 2
                    max_x = WIDTH - b.width // 2
                    b.x = random.randint(min_x, max_x)
            for b in bricks:
                if dudu.colliderect(b) and dudu.bottom < b.bottom:
                    dudu.bottom = b.top
                    on_brick = 1
                    if b.image == '踏板4':
                        dudu.image = '嘟嘟哭'
            if on_brick == 0:
                dudu.y += 8
            if dudu.bottom > HEIGHT:
                dudu.image = '嘟嘟哭'
            if dudu.top < 0:
                dudu.image = '嘟嘟哭'
            if keyboard.left:
                if dudu.left > 0:
                    dudu.x -= 5
            if keyboard.right:
                if dudu.right < WIDTH:
                    dudu.x += 5

    def on_key_down(key):
        if key == keys.SPACE and dudu.image == '嘟嘟哭':
            global score, bricks
            dudu.image = '嘟嘟'
            bricks = []
            score = 0
            for i in range(5):
                n = random.randint(1, 4)
                b = Actor('踏板' + str(n))
                min_x = b.width // 2
                max_x = WIDTH - b.width // 2
                b.x = random.randint(min_x, max_x)
                b.y = 140 * (i + 1)
                bricks.append(b)
                if i == 2:
                    b.image = '踏板1'
                    dudu.x = b.x
                    dudu.bottom = b.top
        
    pgzrun.go()
elif x == '2':
    import pgzrun
    import random
    WIDTH = 500
    HEIGHT = 700

    #创建小核桃
    player = Actor('小核桃')
    player.x = 250
    player.y = 650

    weapons = []
    #创建3个bubu兽角色
    bubus = []
    for i in range(3):
        b = Actor('bubu')
        b.x = random.randint(0, 500)
        bubus.append(b)

    def draw():
        screen.blit('星球', [0, 0])
        player.draw()
        #游戏失败之前绘制角色
        if player.image != '小核桃失败':
            for w in weapons:
                w.draw()
            for b in bubus:
                b.draw()


    def update():
        #小核桃移动
        if keyboard.left:
            player.x -= 7
        if keyboard.right:
            player.x += 7
        if keyboard.up:
            player.y -= 5
        if keyboard.down:
            player.y += 5
        #空格键发射子弹
        if keyboard.space:
            weapon = Actor('子弹')
            weapon.x = player.x
            weapon.y = player.y - 50
            weapons.append(weapon)

        #子弹从下向上移动, 到窗口上边缘消失
        for w in weapons:
            w.y -= 5
        for w in weapons:
            if w.y <= 0:
                weapons.remove(w)
            
        # bubu兽从上向下移动, 到窗口下边后初始化到窗口上方
        for b in bubus:
            b.y += 5
            if b.y > 700:
                b.x = random.randint(0, 500)
                b.y = 0

        #bubu兽和子弹碰撞后, 回到初始位置
        for b in bubus:
            for w in weapons:
                if b.colliderect(w):
                    b.x = random.randint(0, 500)
                    b.y = 0

            #bubu碰到小核桃游戏失败
            if b.colliderect(player):
                player.image = '小核桃失败'
                music.stop()

    music.play('music4')


    pgzrun.go()
elif x == '3':
    import pgzrun
    import random
    WIDTH = 1000
    HEIGHT = 500
    bg1 = Actor('背景图1')
    bg1.x = 500
    bg1.y = 250
    bg2 = Actor('背景图2')
    bg2.x = bg1.x + 1000
    bg2.y = 250
    bird = Actor('月月鸟1')
    bird.x = 200
    bird.y = 100
    magma = Actor('岩浆1')
    magma.x = 500
    magma.y = 480
    b1 = Actor('藤蔓')
    b1.x = 1000
    b1.y = 100
    b2 = Actor('枝干')
    b2.x = 1600
    b2.y = 75
    b3 = Actor('尖刺')
    b3.x = 2100
    b3.y = 400
    blocks = [b1, b2, b3]
    names = ['藤蔓', '枝干', '尖刺']
    num = [100, 75, 400]


    def draw():
        # 1.声明全局变量score
        global score
        bg1.draw()
        bg2.draw()
        bird.draw()
        magma.draw()
        for b in blocks:
            b.draw()
        # 2.显示分数
        screen.draw.text(str(score), (20, 10))


    score = 0


    def update():
        global score
        bg1.x -= 7
        bg2.x -= 7
        if bg1.x < -500:
            bg1.x = bg2.x + 1000
        if bg2.x < -500:
            bg2.x = bg1.x + 1000
        if bird.image == '月月鸟1':
            bird.image = '月月鸟2'
        else:
            bird.image = '月月鸟1'
        if keyboard.space:
            if bird.y > 35:
                bird.y -= 5
        else:
            if bird.y < 465:
                bird.y += 5
        if magma.image == '岩浆1':
            magma.image = '岩浆2'
        else:
            magma.image = '岩浆1'

        for b in blocks:
            b.x -= 7
            if b.x < -30:
                b.x = 1500
                n = random.randint(0, 2)
                b.image = names[n]
                b.y = num[n]
                score += 1

            if bird.colliderect(b):
                print('碰到障碍物')
                exit()

        if bird.colliderect(magma):
            print('碰到岩浆')
            exit()


    music.play('bgmusic')
    pgzrun.go()
elif x == '4':
    # Tic Tac Toe


    import random


    def drawBoard(board):
        # 打印棋盘

        # "board"是长度为10的列表，为了方便输入，忽略第一个元素board[0]

        print('\n\n\n\n')
        print('\t\t\t┌─┬─┬─┐')
        print('\t\t\t│' + board[7] + '│' + board[8] + '│' + board[9] + '│')
        print('\t\t\t├─┼─┼─┤')
        print('\t\t\t│' + board[4] + '│' + board[5] + '│' + board[6] + '│')
        print('\t\t\t├─┼─┼─┤')
        print('\t\t\t│' + board[1] + '│' + board[2] + '│' + board[3] + '│')
        print('\t\t\t└─┴─┴─┘')


    def inputPlayerLetter():
        # 让玩家选择棋子
        # 返回一个列表，第一个是玩家的棋子，第二个是电脑的
        letter = ''
        while not (letter == 'X' or letter == 'O'):
            print('Do you want to be X or O?')
            letter = input().upper()

        if letter == 'X':
            return ['X', 'O']
        else:
            return ['O', 'X']


    def whoGoesFirst():
        # 随机产生谁先走
        if random.randint(0, 1) == 0:
            return 'computer'
        else:
            return 'player'


    def playAgain():
        # 再玩一次？输入yes或y返回True
        print('Do you want to play again? (yes or no)')
        return input().lower().startswith('y')


    def makeMove(board, letter, move):
        # 落子
        board[move] = letter


    def isWinner(bo, le):
        # 判断所给的棋子是否获胜
        # 参数为棋盘上的棋子（列表）和棋子符号
        # 以下是所有可能胜利的情况，共8种
        return ((bo[7] == le and bo[8] == le and bo[9] == le) or
                (bo[4] == le and bo[5] == le and bo[6] == le) or
                (bo[1] == le and bo[2] == le and bo[3] == le) or
                (bo[7] == le and bo[4] == le and bo[1] == le) or
                (bo[8] == le and bo[5] == le and bo[2] == le) or
                (bo[9] == le and bo[6] == le and bo[3] == le) or
                (bo[7] == le and bo[5] == le and bo[3] == le) or
                (bo[9] == le and bo[5] == le and bo[1] == le))


    def getBoardCopy(board):
        # 复制一份棋盘，供电脑落子时使用
        dupeBoard = []

        for i in board:
            dupeBoard.append(i)

        return dupeBoard


    def isSpaceFree(board, move):
        # 判断这个位置是否有子，没子返回True
        return board[move] == ' '


    def getPlayerMove(board):
        # 玩家落子
        move = ' '
        while move not in '1 2 3 4 5 6 7 8 9'.split() or not isSpaceFree(board, int(move)):
            print('What is your next move? (1-9)')
            move = input()
        return int(move)


    def chooseRandomMoveFromList(board, movesList):
        # 随机返回一个可以落子的坐标
        # 如果没有所给的movesList中没有可以落子的，返回None
        possibleMoves = []
        for i in movesList:
            if isSpaceFree(board, i):
                possibleMoves.append(i)

        if len(possibleMoves) != 0:
            return random.choice(possibleMoves)
        else:
            return None


    def getComputerMove(board, computerLetter):
        # 确定电脑的落子位置
        if computerLetter == 'X':
            playerLetter = 'O'
        else:
            playerLetter = 'X'

        # Tic Tac Toe AI核心算法:
        # 首先判断电脑方能否通过一次落子直接获得游戏胜利
        for i in range(1, 10):
            copy = getBoardCopy(board)
            if isSpaceFree(copy, i):
                makeMove(copy, computerLetter, i)
                if isWinner(copy, computerLetter):
                    return i

        # 判断玩家下一次落子能否获得胜利，如果能，给它堵上
        for i in range(1, 10):
            copy = getBoardCopy(board)
            if isSpaceFree(copy, i):
                makeMove(copy, playerLetter, i)
                if isWinner(copy, playerLetter):
                    return i

        # 如果角上能落子的话，在角上落子
        move = chooseRandomMoveFromList(board, [1, 3, 7, 9])
        if move != None:
            return move

        # 如果能在中心落子的话，在中心落子
        if isSpaceFree(board, 5):
            return 5

        # 在边上落子
        return chooseRandomMoveFromList(board, [2, 4, 6, 8])


    def isBoardFull(board):
        # 如果棋盘满了，返回True
        for i in range(1, 10):
            if isSpaceFree(board, i):
                return False
        return True


    print('Welcome to Tic Tac Toe!')

    while True:
        # 更新棋盘
        theBoard = [' '] * 10
        playerLetter, computerLetter = inputPlayerLetter()
        turn = whoGoesFirst()
        print('The ' + turn + ' will go first.')
        gameIsPlaying = True

        while gameIsPlaying:
            if turn == 'player':
                # 玩家回合
                drawBoard(theBoard)
                move = getPlayerMove(theBoard)
                makeMove(theBoard, playerLetter, move)

                if isWinner(theBoard, playerLetter):
                    drawBoard(theBoard)
                    print('Hooray! You have won the game!')
                    gameIsPlaying = False
                else:
                    if isBoardFull(theBoard):
                        drawBoard(theBoard)
                        print('The game is a tie!')
                        break
                    else:
                        turn = 'computer'


            else:
                # 电脑回合
                move = getComputerMove(theBoard, computerLetter)
                makeMove(theBoard, computerLetter, move)

                if isWinner(theBoard, computerLetter):
                    drawBoard(theBoard)
                    print('The computer has beaten you! You lose.')
                    gameIsPlaying = False
                else:
                    if isBoardFull(theBoard):
                        drawBoard(theBoard)
                        print('The game is a tie!')
                        break
                    else:
                        turn = 'player'

        if not playAgain():
            break